package com.milotnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milotnt.mapper.ClassOrderMapper;
import com.milotnt.pojo.ClassOrderDto;
import com.milotnt.pojo.ClassOrder;
import com.milotnt.pojo.ClassTable;
import com.milotnt.service.ClassOrderService;
import com.milotnt.service.ClassTableService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ZhangMing [1157038410@qq.com]
 * @date 2021/8/11
 */

@Service
public class ClassOrderServiceImpl extends ServiceImpl<ClassOrderMapper, ClassOrder> implements ClassOrderService {

    @Autowired
    private ClassOrderMapper classOrderMapper;

    @Autowired
    private ClassTableService tableService;

    @Override
    public List<ClassOrder> findAll() {
        return classOrderMapper.findAll();
    }

    @Override
    public Boolean insertClassOrder(ClassOrder classOrder) {
        return this.save(classOrder);
    }

    @Override
    public List<ClassOrderDto> selectClassOrderByMemberAccount(Integer memberAccount) {
        LambdaQueryWrapper<ClassOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassOrder::getMemberAccount, memberAccount);
        lqw.orderByDesc(ClassOrder::getClassBegin);
        List<ClassOrder> orders = this.list(lqw);
        List<ClassOrderDto> res = new ArrayList<>();
        for (ClassOrder order : orders) {
            ClassTable table = tableService.getById(order.getClassId());
            Integer status = table.getStatus();
            ClassOrderDto dto = new ClassOrderDto();
            BeanUtils.copyProperties(order, dto);
            dto.setStatus(status);
            res.add(dto);
        }
        return res;
    }

    @Override
    public Boolean deleteByClassOrderId(Integer classOrderId) {
        return this.removeById(classOrderId);
    }

    @Override
    public ClassOrder selectMemberByClassIdAndMemberAccount(Integer classId, Integer memberAccount) {
        LambdaQueryWrapper<ClassOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassOrder::getClassId, classId);
        lqw.eq(ClassOrder::getMemberAccount, memberAccount);
        return this.getOne(lqw);
    }

    @Override
    public List<ClassOrder> selectMemberOrderList(Integer classId) {
        LambdaQueryWrapper<ClassOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassOrder::getClassId, classId);
        return this.list(lqw);
    }

    @Override
    public List<ClassOrder> recentClass(Integer memberAccount) {
        LambdaQueryWrapper<ClassOrder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ClassOrder::getMemberAccount, memberAccount);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String s = format.format(new Date());
        lqw.gt(ClassOrder::getClassBegin, s);
        lqw.orderByAsc(ClassOrder::getClassBegin);
        lqw.last("limit 5");
        return this.list(lqw);
    }
}
